package com.sqx.common.aspect;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Set;

@Component
@Aspect
@Slf4j
public class RequestLog {

    @Around(value = "@annotation(org.springframework.web.bind.annotation.PostMapping)", argNames = "jp")
    public Object logAopWritePost(ProceedingJoinPoint jp) throws Throwable {
        return logAopWrite(jp);
    }

    @Around(value = "@annotation(org.springframework.web.bind.annotation.GetMapping)", argNames = "jp")
    public Object logAopWriteGet(ProceedingJoinPoint jp) throws Throwable {
        return logAopWrite(jp);
    }

    @Around(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)", argNames = "jp")
    public Object logAopWriteRqGet(ProceedingJoinPoint jp) throws Throwable {
        return logAopWrite(jp);
    }
    @Around(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)", argNames = "jp")
    public Object logAopWrite(ProceedingJoinPoint jp) throws Throwable {
        long curtime = System.currentTimeMillis();
        Object[] args = jp.getArgs();
        HttpServletRequest request = null;
        try {
            for (Object obj : args) {
                if (obj instanceof HttpServletRequest) {
                    request = (HttpServletRequest) obj;
                }
            }
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if (requestAttributes == null) {
                return jp.proceed();
            }
            if (request == null) {
                request = ((ServletRequestAttributes) requestAttributes).getRequest();
            }
        } catch (Exception e) {
            log.error("", e);
            throw e;
        }
        StringBuilder sbstr = new StringBuilder();
        try {
            Set<String> keyset = request.getParameterMap().keySet();
            for (String key : keyset) {
                String[] values = request.getParameterValues(key);
                for (String value : values) {
                    sbstr.append(key).append("=").append(value).append("&");
                }
            }
            Object object = jp.proceed();// round to

            if(!StringUtils.contains(request.getRequestURL().toString(),"helth")){
                String jspstr = JSON.toJSONString(object);
                if (jspstr.length() > 4089) {
                    jspstr = jspstr.substring(0, 4089);
                }
                String rspStrs = request.getRequestURL().toString() + "?" + sbstr.toString() + "\t" + jspstr + "\tcost [" + (System.currentTimeMillis() - curtime) + "]";
                log.info(rspStrs);
            }
            return object;
        } catch (Exception e) {
            log.error("", e);
            throw e;
        }
    }
}
